/*
 * Copyright (c) 2002-2012 Alibaba Group Holding Limited.
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hd.assistant.web.home.module.screen;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.citrus.turbine.Context;
import com.alibaba.citrus.turbine.Navigator;
import com.alibaba.citrus.turbine.dataresolver.Param;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hd.assistant.biz.service.exception.ServiceException;
import com.hd.assistant.biz.service.query.PaipaiItemSearchQuery;
import com.hd.assistant.biz.util.PaipaiUtil;
import com.hd.assistant.dal.dataobject.User;
import com.hd.assistant.web.common.base.BaseModule;
import com.hd.assistant.web.common.util.ClickedUtil;

/**
 * 外部重定向，用来示范redirect token的使用方法。
 *
 * @author Michael Zhou
 */
public class ItemList extends BaseModule {
    private final static Logger log = LoggerFactory.getLogger(ItemList.class);

    @Autowired
    private HttpSession session;
    
    
    public void execute(
    		@Param("appType") String appType, 
    		@Param("pindex") String pindex,
    		@Param("index") String index,
    		@Param("type") String type, 
    		@Param("page") Integer page, 
    		@Param("pageSize") Integer pageSize, 
    		@Param("query") String queryStr,
    		@Param("nextPage") String nextPage,
    		@Param("pageTotal") Integer pageTotal,
    		@Param("itemstatus") Integer itemstatus, 
    		@Param("orderby") Integer orderby, 
    		Context context,Navigator nav) {
    	try{
    		context.put("pindex", pindex==null?1:pindex);
        	context.put("index", index==null?100:index);
        	context.put("type",type);
        	context.put("query",queryStr);
        	context.put("itemstatus",itemstatus);
        	context.put("orderby",orderby);
        	User user = checkPermision(appType,session,context,nav);
        	
        	//来源来自拍拍
        	if(user != null && user.getUserType().equals(User.Type_PAIPAI)){
	        		PaipaiItemSearchQuery query = new PaipaiItemSearchQuery();
	        		query.setPageSize(pageSize==null?10:pageSize);
	        		if(nextPage != null && nextPage.equals("pre"))
	        			page = (page == 1)? 1:page-1;
	        		if(nextPage != null && nextPage.equals("next"))
	        			page = (page==pageTotal)?pageTotal:page+1;
	        		
	        		query.setPageIndex(page==null?1:page);
	        		if(queryStr != null)
	        			query.setItemName(queryStr);
	        		if(itemstatus != null && itemstatus > 0){
	        			query.setItemState(itemstatus);
	        		}
	        		if(orderby != null &&orderby >= 0){
	        			query.setOrderType(orderby);
	        		}
	        		try{
	        			JSONObject results = paiPaiIsvService.loadSellerItems(query, user);
	        			generateClickedId(results);
	        			context.put("results", results);
	        		}catch(ServiceException se){
	            		log.error("error:",se);
	            		if(se.needReLogin){
	            			nav.redirectToLocation(PaipaiUtil.getRedirectLoginUrl(PaipaiUtil.APP_OAUTH_ID_ASSISTANT));
	            		}else{
	            			nav.forwardTo("error");
	            		}
	            		
	            	}
        	}
    	}catch(Exception e){
    		log.error("error:",e);
    	}
        	

    }

	private void generateClickedId(JSONObject results) {
		for(Object item :(JSONArray)results.get("itemList")){
			if(item instanceof JSONObject){
				String itemCode = ((JSONObject) item).getString("itemCode");
				String clickedId = ClickedUtil.getClickedId(User.Type_PAIPAI, itemCode);
				((JSONObject) item).put("clickedId", clickedId);
			}
		}
	}
    
   
}
